package com.tianqu.tms.www.common.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.LoginException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.security.TmsUserProfileFactory;
import com.tianqu.tms.core.util.MenuTagUtil;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn0000Bo;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.DectCodes;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.entity.UserAuth;
import com.tianqu.tms.entity.UserRole;
import com.tianqu.tms.entity.VehicleBrand;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.common.actionform.LoginActionForm;

import javax.servlet.http.HttpServletRequest;

/**
 * login controller
 * 
 * @author liugang
 * 
 */
public class LoginController extends TmsController {

	private Logger logger = Logger.getLogger(LoginController.class);

	/*
	 * login业务逻辑类，处理登录信息是否正确 (non-Javadoc)
	 */
	public void execute(UserContext uc) throws TmsException {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		controller.begin();
		LoginActionForm form = (LoginActionForm) uc.getInputForm();
		// userprofile初始化
		TmsUserProfileFactory userfactory = TmsUserProfileFactory
				.getInstance();
		TmsUserProfile userprofile = userfactory.getUserProfile(form
				.getUserId());

		// 判断登录是否成功
		User user = (User) userfactory.getTmsUser(form.getUserId(), form
				.getPassword());
		// 保存到UC里面
		uc.setUserProfile(userprofile);
		if (user == null) {
			logger.error("Login Failed: not found loginID="
					+ form.getUserId());
			throw new LoginException(LoginException.LOGIN_EXCEPTION, this
					.getClass().getName()
					+ ".valudateLogin");

		} else {// 验证密码 已删除或
			if (user.getFlag() == 2) {
				logger.error("User Failed: 此用户已被删除=" + form.getUserId());
				throw new LoginException(LoginException.LOGIN_EXCEPTION, this
						.getClass().getName()
						+ ".valudateUserId");
			} else if (!user.getPassword().equals(form.getPassword())) {
				logger.error("Password Failed: error password="
						+ form.getPassword());
				throw new LoginException(
						LoginException.LOGIN_PASSWORD_EXCEPTION, this
								.getClass().getName()
								+ ".valudatePassword");
			} else if (user.getFlag() == 3) {
				// 禁用状态 向前台打出提示信息 该帐号已被禁止
				logger.error("User Failed: 已禁用用户=" + form.getUserId());
				throw new LoginException(
						LoginException.LOGIN_STATUS_EXCEPTION, this
								.getClass().getName()
								+ ".valudateFlag");
			}
		}

		// 获得用户权限组信息
		//这块直接从role表中获取值,根据userId获取所有的roleId
		UserAuth auth = UserAuth.getInstance();
		List<UserAuth> list = auth.getAuth(user.getId());
		
		List funcId = new ArrayList();
		for (UserAuth Auth : list) {
			
			//根据roleId获取funcId
			UserRole role =new UserRole();
			role = role.findRoleByRole(Auth.getrId());
			if(role.getFuncIds()!=null)
			{
				String[] bb = role.getFuncIds().split(",");
				List yyList = Arrays.asList(bb);	
				for(int i=0;i<yyList.size();i++)
				{
					funcId.add(yyList.get(i));
				}
			}
		}

		// Userprofile赋值
		setUserProfile(userprofile, user, funcId);

		// 保存到UC里面
		uc.setUserProfile(userprofile);
		/*
		 * 加载字典表
		 */
		Codes.getInstance();
		/**
		 * 车辆品牌字典表
		 */
		// CarCodes.getInstance();
		// 品牌
		Vendor.getInstance();
		// 车系
		VehicleBrand.getInstance();
		// 款式
		CarStyle.getInstance();
		//大车品牌车系款式
		CommercialVehicle.getInstance();
		// 检测
		DectCodes.getInstance();
		controller.commit();
		logger.info("LoginId : " + form.getUserId() + " 登录成功!");
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	/**
	 * TmsUserProfile的设置
	 * 
	 * @param userprofile
	 * @param user
	 * @param list
	 */
	void setUserProfile(TmsUserProfile userprofile, User user, List list) {
		userprofile.setUniqueId((int) user.getId());
		userprofile.setLoginId(user.getLoginId());
		// 真实姓名
		userprofile.setRealName(user.getRealName());
		userprofile.setUserClass(Integer.parseInt(user.getUcId()));// 权限类型
		userprofile.setUserClassList(list);
		userprofile.setUserType(Integer.parseInt(user.getUserType()));
	}
	
	public static void main(String[] args) {
		String a = "11,23,33,44,55,66";
		
		String[] bb = a.split(",");
		
		List list = Arrays.asList(bb);
		
		System.out.println(list.get(1)); 
	} 
}
